package com.sys.servlet;


import com.sys.entity.FitnessEquipment;
import com.sys.service.FitnessEquipmentService;
import com.sys.service.impl.FitnessEquipmentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

@WebServlet("/fAdd")
public class ServletAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out=response.getWriter();

        FitnessEquipmentService dao=new FitnessEquipmentServiceImpl();
        FitnessEquipment fitnessEquipment=new FitnessEquipment();
        fitnessEquipment.setName(request.getParameter("name"));
        fitnessEquipment.setPrice(Double.valueOf(request.getParameter("price")));;
        fitnessEquipment.setCount(Integer.parseInt(request.getParameter("count")));
        fitnessEquipment.setStatus(Integer.parseInt(request.getParameter("status")));
        fitnessEquipment.setBuyTime(Date.valueOf(request.getParameter("buy_time")));
        int number=dao.add(fitnessEquipment);
        if (number>0){
            out.print("<script>alert('新增成功！');location.href='fShow'</script>");
        }else{
            out.print("<script>alert('新增失败！');location.href='FeAdd.jsp'</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
